package cn.tedu.boot43.controller;

import cn.tedu.boot43.entity.Product;
import cn.tedu.boot43.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {

    @Autowired
    ProductMapper mapper;

    @RequestMapping("/insert")
    public int insert(@RequestBody Product product){
        System.out.println("product = " + product);
        if (product.getTitle()==null||product.getPrice()==null||product.getSaleCount()==null){
            return 1;
        }
        mapper.insert(product);
        return 2;
    }

    @RequestMapping("/select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void deletePro(int id){
        mapper.deletePro(id);
    }

    @RequestMapping("/selectById")
    public Product selectById(int id){

        return mapper.selectById(id);
    }

    @RequestMapping("/update")
    public int update(@RequestBody Product product){
        System.out.println("product = " + product);
        if (product.getTitle()==null||product.getPrice()==null||product.getSaleCount()==null){
            return 1;
        }
        mapper.update(product);
        return 2;
    }

}
